package com.lubase.wfengine.node.task;

import com.lubase.model.DbCollection;
import com.lubase.orm.util.TypeConverterUtils;
import com.lubase.wfengine.auto.entity.WfFInsEntity;
import com.lubase.wfengine.auto.entity.WfTEventEntity;
import com.lubase.wfengine.auto.entity.WfTInsEntity;
import com.lubase.wfengine.auto.entity.WfTaskEntity;
import com.lubase.wfengine.dao.WfTaskDao;
import com.lubase.wfengine.model.*;
import com.lubase.wfengine.node.BaseNodeService;
import com.lubase.wfengine.remote.SendEventToRemoteService;
import com.lubase.wfengine.service.BisDataUpdateService;
import com.lubase.wfengine.service.WorkTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ParallelGatewayService extends BaseNodeService {
    @Override
    public ETaskType getTaskType() {
        return ETaskType.ParallelGateWay;
    }

    @Autowired
    WorkTaskService taskService;
    @Autowired
    WfTaskDao wfTaskDao;
    @Autowired
    BisDataUpdateService bisDataUpdateService;
    @Autowired
    SendEventToRemoteService sendEventToRemoteService;

    /**
     * 并行网关，成对使用
     * 1. 判断连接到此节点的所有线是否完成，如果完成则执行此网关任务
     * 2. 执行任务时，选择所有的后续节点，生成任务
     *
     * @param fIns
     * @param taskEntity
     * @param tIns
     * @param event
     * @param bisData
     * @return
     */
    @Override
    public Boolean runTask(WfFInsEntity fIns, WfTaskEntity taskEntity, WfTInsEntity tIns, WfTEventEntity event, DbCollection bisData) {
        //如果是并行网关节点，需要找到所有的前置节点，判断是否完成
        List<WfTInsEntity> beforeTInsList = taskService.getBeforeTaskInsList(event.getFins_id(), taskEntity);
        boolean isAllCompleted = beforeTInsList.stream().noneMatch(tInsEntity -> tInsEntity.getProcess_status().equals(EProcessStatus.UnProcess.getStatus()));
        if (isAllCompleted) {
            //前置节点都完成后，需要找到所有的后续节点，然后生成对应的任务
            List<NextTaskEntity> nextTasks = taskService.getNextTasks2(taskEntity, bisData, null);
            //创建下一个节点

            for (WfTaskEntity nextTask : nextTasks) {
                //  创建下一个节点的实例，并且生成event任务
                WfTInsEntity nextTaskTIns = wfTaskDao.createTaskIns(fIns, nextTask, taskEntity);
                createEventJob(fIns, nextTaskTIns);
                //结束节点不广播节点创建事件
                if (nextTask.getTask_type().equals(ETaskType.EndEvent.getType())) {
                    continue;
                }

                OpenEventModel taskCreateEventModel = new OpenEventModel(EOpenEventType.CreateTIns, fIns.getService_id(), fIns.getData_id(), fIns.getId().toString());
                taskCreateEventModel.setTaskId(tIns.getTask_id());
                taskCreateEventModel.setTaskInsId(tIns.getId().toString());
                sendEventToRemoteService.sendWorkFlowOpenEventToCache(taskCreateEventModel);
            }
            //执行link上配置的更新条件
            for (NextTaskEntity nextTaskEntity : nextTasks) {
                String memo = nextTaskEntity.getFromLink().getId() + TypeConverterUtils.object2String(nextTaskEntity.getFromLink().getMemo(), "");
                bisDataUpdateService.sendLinkEvent(fIns, nextTaskEntity.getFromLink().getUpdate_content(), memo);
            }
        }
        //2、更新任务实例状态（如果需要）
        wfTaskDao.updateTInsStatus(tIns.getId());
        return true;
    }
}
